package seed

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/webserver/controllers/util"
	"github.com/gin-gonic/gin"
)

func Delete(c *gin.Context) {
	_, err := util.AdminFilter(c)
	if err != nil {
		return
	}

	seedID, err := util.PostFormInt(c, "seed_id")
	if err != nil {
		return
	}

	tx := db.DB.Begin()
	if err := tx.Delete(&models.NsqSetting{}, "seed_id = ?", seedID).
		Error; err != nil {
		tx.Rollback()
		util.FailedResponse(c, 10001, "解除绑定失败")
		return
	}

	if err := tx.Delete(&models.Seed{}, "id = ?", seedID).Error; err != nil {
		tx.Rollback()
		util.FailedResponse(c, 10002, "删除任务失败")
		return
	}

	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		util.FailedResponse(c, 10003, "删除任务失败")
		return
	}

	util.SuccessResponse(c)
}
